#include "ZipNavigator.h"
#include "UnitTest++.h"
#include "ZipManager.h"

using namespace std;
const char* filePath = "resources/file.zip";
char buffer[1024];
ZipManager* manager = new ZipManager();

void TestNavigationBuffer(FileHeader* fileHeader)
{
    const char* expectedFormatString = "Name: %s\nModified Time: %s\nCompression Percentage: %.2f %\n\n";
    float compressionPercentage = fileHeader->calculateCompressionPercentage();
    char aux[100];
    sprintf(aux, expectedFormatString,
            fileHeader->fileName_, fileHeader->parseTimeToString().c_str(), compressionPercentage);
    strcat(buffer, aux);
}

TEST(ShouldReturnNameCompressionPercentageModificationTimeOfAFile)
{
    manager->openFile(filePath);
    manager->traverse(TestNavigationBuffer);
    string expected("Name: file1.txt\nModified Time: Wed May 15 18:22:32 2013\nCompression Percentage: 0.00 %\n\n");
    CHECK_EQUAL(expected.c_str(), buffer);
    memset(&buffer, 0, strlen(buffer));
}

TEST(ShouldReturnNameCompressionPercentageModificationTimeOfTwoFiles)
{
    manager->openFile("resources/twofiles2.zip");
    manager->traverse(TestNavigationBuffer);
    string expected("Name: file4.txt\nModified Time: Mon May 20 15:19:00 2013\nCompression Percentage: 0.00 %\n\nName: file3.txt\nModified Time: Mon May 20 15:18:42 2013\nCompression Percentage: 0.00 %\n\n");
    CHECK_EQUAL(expected.c_str(), buffer);
}

TEST(ShouldReturn0PercentOfCompression)
{
    ZipReader* reader = new ZipReader(filePath);
    float expectedPercentage = 0;

    FileHeader* fileHeader = reader->extractFileHeaders().front();
    CHECK_EQUAL(expectedPercentage, fileHeader->calculateCompressionPercentage());
    delete reader;
}

TEST(ShouldReturnTheLastModifiedTimeOfAZipFileReadable)
{
    ZipReader* reader = new ZipReader(filePath);

    FileHeader* actualFileHeader = reader->extractFileHeaderFields(0);

    string expectedModifiedTime = "Wed May 15 18:22:32 2013 ";
    string actualModifiedTime = actualFileHeader->parseTimeToString();

    CHECK(expectedModifiedTime.compare(actualModifiedTime) == 1);
}
